
function Ship(head,tail){
	this.headCoordinateX=head.substring(0,1);
	this.headCoordinateY=parseInt(head.substring(1));
	this.tailCoordinateX=tail.substring(0,1);
	this.tailCoordinateY=parseInt(tail.substring(1));
	this.orientation=0;//0 for vertical, 1 for horizontal
	this.collisionZone=[];
	this.shipLocations=[];
	this.smallerCoordinate="";
	var subtracted = 0;
	if(this.headCoordinateX==this.tailCoordinateX){
		subtracted=this.headCoordinateY-this.tailCoordinateY;
		this.orientation=1; // X coordinate is always the same
	} else {
		subtracted=LETTERS.indexOf(this.headCoordinateX)-LETTERS.indexOf(this.tailCoordinateX);
		this.orientation=0;// just in case, Y coordinate is always the same
	}
	if(subtracted<0){
		this.smallerCoordinate=this.headCoordinateX+this.headCoordinateY;
	} else {
		this.smallerCoordinate=this.tailCoordinateX+this.tailCoordinateY;
	}
	this.size=Math.abs(subtracted)+1;
	this.health=this.size;
	var locationStartNumber=0;
	
	// calculate the coordiantes this ship occupies
	if(this.orientation==1){
		locationStartNumber=parseInt(this.smallerCoordinate.substring(1));
	} else {
		locationStartNumber=LETTERS.indexOf(this.smallerCoordinate.substring(0, 1));
	}
	for(var i=locationStartNumber;i<(locationStartNumber+this.size);i++){
		if(this.orientation==1){
			this.shipLocations.push(this.headCoordinateX+i);
		} else {
			this.shipLocations.push(LETTERS.charAt(i)+this.headCoordinateY);
		}
	}
	
	// fill collisionZone
	for(var q=0;q<3;q++){
		for(var w=0;w<3;w++){
			var headX=(LETTERS.indexOf(this.headCoordinateX)-1+q);
			var headY=(this.headCoordinateY-1+w);
			if(headX>=0 && headY>=1){
				this.collisionZone.push(LETTERS.charAt(headX) + headY);
			}
			var tailX=(LETTERS.indexOf(this.tailCoordinateX)-1+q);
			var tailY=(this.tailCoordinateY-1+w);
			if(tailX>=0 && tailY>=1){
				this.collisionZone.push(LETTERS.charAt(tailX) + tailY);
			}
		}
	}
}

Ship.prototype.isValidSize = function(){
	return this.size<5 && this.size>0 && (this.headCoordinateX==this.tailCoordinateX || this.headCoordinateY==this.tailCoordinateY);
};

Ship.prototype.toString=function(){
	return this.size + " " + this.headCoordinateX + this.headCoordinateY + ":"  + this.tailCoordinateX + this.tailCoordinateY + (this.orientation==1?" horizontal":" vertical");
};

Ship.prototype.isCollision = function(coordinate){
	return jQuery.inArray(coordinate,this.collisionZone)>=0;
};

Ship.prototype.isHit=function(coordinate){
	return jQuery.inArray(coordinate,this.shipLocations)>=0;
};

//returns true if ship was bombed down
Ship.prototype.bomb=function(coordinate){
	if(this.isHit(coordinate)){
		this.health--;
	}
	if(this.health==0){
		return true;
	} 
	return false;
};
